package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.MemberDao;
import ysq.xyg.model.Member;
import ysq.xyg.service.BaseConfigService;
import ysq.xyg.service.MemberService;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:11
 **/
@Service("memberService")
public class MemberServiceImpl extends BaseServiceImpl<Member> implements MemberService {

    @Resource
    private BaseConfigService baseConfigService;
    @Resource
    private MemberDao memberDao;
    @Autowired
    public void setMemberDao(MemberDao memberDao) {
        this.baseDao = memberDao;
    }

    @Override
    public PageInfo<Member> pageEntityInfo(Member member, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        if(member!=null){
            if(StringUtil.isNotEmpty(member.getNickName())){
                member.setNickName(BaseUtil.encodeBase64String(member.getNickName()));
            }
            if(StringUtil.isNotEmpty(member.getIntroductName())){
                member.setIntroductName(BaseUtil.encodeBase64String(member.getIntroductName()));
            }
            if(StringUtil.isNotEmpty(member.getOneUserName())){
                member.setOneUserName(BaseUtil.encodeBase64String(member.getOneUserName()));
            }
        }
        List<Member> list = memberDao.selectEntity(member);
        return new PageInfo<Member>(list);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo<Member> pageInfo(Member member, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(member.getPhone())){
            criteria.andLike("phone","%"+member.getPhone()+"%");
        }
        if (StringUtil.isNotEmpty(member.getMemberIds())){
            String memberIds = member.getMemberIds();
            String[] memberIdArr = memberIds.split(",");
            List<String> memberIdList = new ArrayList<String>();
            if (memberIdArr != null && memberIdArr.length > 0){
                for (String memberId : memberIdArr) {
                    memberIdList.add(memberId);
                }
            }
            criteria.andNotIn("id", memberIdList);
        }
        if(StringUtil.isNotEmpty(member.getName())){
            criteria.andLike("name","%"+member.getName()+"%");
        }
        if(StringUtil.isNotEmpty(member.getNickName())){
            criteria.andLike("nickName", "%"+ BaseUtil.encodeBase64String(member.getNickName())+"%");
        }
        if(StringUtil.isNotEmpty(member.getId())){
            criteria.andLike("id","%"+member.getId()+"%");
        }
        if(StringUtil.isNotEmpty(member.getIntroductUserId())){
            criteria.andEqualTo("introductUserId",member.getIntroductUserId());
        }
        if(StringUtil.isNotEmpty(member.getOneUserId())){
            criteria.andEqualTo("oneUserId",member.getOneUserId());
        }
        if(StringUtil.isNotEmpty(member.getAreaUserId())){
            criteria.andLike("areaUserId","%"+member.getAreaUserId()+"%");
        }

        if(StringUtil.isNotEmpty(member.getCardNo())){
            criteria.andLike("cardNo","%"+member.getCardNo()+"%");
        }
        if(StringUtil.isNotEmpty(member.getGrade())){
            criteria.andEqualTo("grade",member.getGrade());
        }

        List<Member> list=memberDao.selectByExample(example);
        return new PageInfo<Member>(list);
    }

    @Override
    public List<Member> list(Member member) {

        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(member.getPhone())){
            criteria.andLike("phone","%"+member.getPhone()+"%");
        }
        if(StringUtil.isNotEmpty(member.getName())){
            criteria.andEqualTo("name",member.getName());
        }
        if(StringUtil.isNotEmpty(member.getOneUserId())){
            criteria.andEqualTo("oneUserId",member.getOneUserId());
        }
        if(StringUtil.isNotEmpty(member.getIntroductUserId())){
            criteria.andEqualTo("introductUserId",member.getIntroductUserId());
        }
        if(StringUtil.isNotEmpty(member.getGrade())){
            criteria.andEqualTo("grade",member.getGrade());
        }
        if(StringUtil.isNotEmpty(member.getNickName())){
            criteria.andLike("nickName", "%"+ BaseUtil.encodeBase64String(member.getNickName())+"%");
        }
        List<Member> list=memberDao.selectByExample(example);
        return list;
    }

    @Override
    public Member selectEntityByOpenid(String openid) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("openid",openid);
        List<Member> list=memberDao.selectByExample(example);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }



    @Override
    public List<Member> selectEntity(Member member) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(member.getUnionid())){
            criteria.andEqualTo("unionid", member.getUnionid());
        }
        if(StringUtil.isNotEmpty(member.getPhone())){
            criteria.andEqualTo("phone", member.getPhone());
        }
        List<Member> list=memberDao.selectByExample(example);
        return list;
    }

    @Override
    public int selectMaxId() {
        return memberDao.selectMaxId();
    }

    /**
     * 查询数量
     */
    @Override
    public int selectMemberCount(Member member){
        return memberDao.selectMemberCount(member);
    }



}
